import {createRouter, createWebHistory} from "vue-router";
//路由数组
const routes = [
    {
        path: "/",
        redirect: "/login"
    },
    {
        path: "/login",
        name: "login",
        component: () => import("./page/Login1.vue")
    },
    {
        path: "/register",
        name: "register",
        component: () => import("./page/registerPage.vue")
    },
    {
        path: "/index",
        name: "index",
        component: () => import("./page/Index.vue"),


        children: [
            {
                path: "",
                components: {
                    head: import("./page/TopPage.vue"),
                    default: import("./page/BodyPage.vue")
                },
                children: [
                    {
                        path: "home",
                        name: "home",
                        component: () => import("./page/homePage.vue")
                    },
                    {
                        path: "FilePage",
                        name: 'FilePage',
                        component: () => import("./page/FilePage.vue")
                    },
                    {
                        path: "RecyclePage",
                        name: 'RecyclePage',
                        component: () => import("./page/RecyclePage.vue")
                    },
                    {
                        path:"SharePage",
                        name:'SharePage',
                        component: () => import("./page/SharePage.vue")
                    },
                    {
                        path:"ReceiveShare",
                        name:'ReceiveShare',
                        component: () => import("./page/ReceiveShare.vue")
                    },
                    {
                        path:"MyCollection",
                        name:'MyCollection',
                        component: () => import("./page/MyCollection.vue")
                    }
                ]
            }

        ]

    },

]
//路由对象
const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes //上面的路由数组
})
//导出路由对象，在 main.js 中引用
router.beforeEach(function (to, from, next) {
    if (to.path === '/login' || to.path === '/register') {
        return next();
    }
    const tokenStr = window.sessionStorage.getItem('userName')
    if (tokenStr) {
        return next();
    }
    next('/login');
})
export default router